package com.ailot.cloud.upms.api.controller;


import com.ailot.cloud.base.common.constants.CommonConstant;
import com.ailot.cloud.base.common.enums.MenuTypeEnum;
import com.ailot.cloud.base.security.utils.SecurityUtil;
import com.ailot.cloud.upms.api.service.SysMenuService;
import com.ailot.cloud.upms.api.service.SysRoleService;
import com.ailot.cloud.upms.common.dto.MenuTree;
import com.ailot.cloud.upms.common.entity.SysMenu;
import com.ailot.cloud.upms.common.util.TreeUtil;
import lombok.AllArgsConstructor;
import org.assertj.core.util.Sets;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("/menu")
public class SysMenuController {
    private final SysMenuService sysMenuService;
    private final SysRoleService sysRoleService;


    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public SysMenu getById(@PathVariable String id) {
        return sysMenuService.getById(id);
    }


    @PostMapping
    @PreAuthorize("hasAuthority('sys:menu:add')")
    public void add(@Valid @RequestBody SysMenu sysMenu) {
        sysMenuService.addMenu(sysMenu);
    }


    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:del')")
    public Boolean delete(@PathVariable String id) {
        return sysMenuService.removeById(id);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sys:menu:edit')")
    public Boolean edit(@Valid @RequestBody SysMenu sysMenu) {
        return sysMenuService.updateById(sysMenu);
    }

    /**
     * 获取当前用户的菜单
     */
    @GetMapping("/getMenuList")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public List<MenuTree> getUserMenu() {
        Set<SysMenu> menus = Sets.newHashSet();
        String id = SecurityUtil.getUser().getId();
        sysRoleService.getRoleIdsByUserId(id).stream().forEach(roleId -> menus.addAll(sysMenuService.getMenusByRoleId(roleId)));
        List<MenuTree> menuTreeList = menus.stream()
                .filter(menu -> MenuTypeEnum.MENU.getCode().equals(menu.getType()))
                .map(MenuTree::new)
                .sorted(Comparator.comparingInt(MenuTree::getSort))
                .collect(Collectors.toList());
        return TreeUtil.build(menuTreeList, CommonConstant.PARENT_ID);
    }

    /**
     * 获取角色菜单集合
     *
     * @return
     */
    @GetMapping("/getRoleMenuIds/{roleId}")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public List<String> getRoleMenuIds(@PathVariable String roleId) {
        List<SysMenu> menuList = sysMenuService.getMenusByRoleId(roleId);
        return menuList.stream().map(SysMenu::getId).collect(Collectors.toList());
    }

    /**
     * 获取当前用户的菜单和按钮
     */
    @GetMapping("/getAllMenuList")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public List<MenuTree> getTree() {
        Set<SysMenu> menus = Sets.newHashSet();
        String id = SecurityUtil.getUser().getId();
        sysRoleService.getRoleIdsByUserId(id).stream().forEach(roleId -> menus.addAll(sysMenuService.getMenusByRoleId(roleId)));
        List<MenuTree> menuTreeList = menus.stream()
                .map(MenuTree::new)
                .sorted(Comparator.comparingInt(MenuTree::getSort))
                .collect(Collectors.toList());
        return TreeUtil.build(menuTreeList, CommonConstant.PARENT_ID);
    }

}
